package com.alpha.processor.processor.tools;

import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.ProcessingEnvironment;

public class ProcessorTool {
    private ProcessingEnvironment processingEnv;
    private List<String> args = new ArrayList<>();

    private boolean printLog = true;

    public ProcessorTool(ProcessingEnvironment env) {
        this.processingEnv = env;
    }

    public void updateLogController(boolean printLog){
        this.printLog = printLog;
    }

    public ProcessorTool addArgs(String arg) {
        if (printLog){
            args.add(arg);
        }
        return this;
    }

    /**
     *  Print Log
     */
    public void printLog() {
        if (!printLog){
            return;
        }
        TypeSpec.Builder builder = TypeSpec.classBuilder("Logger");

        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("printLogger");

        int len = args.size();
        for (int i = 0; i < len; i++) {
            String arg = args.get(i);
            methodBuilder.addCode("\tString arg_" + i + " = \"" + arg + "\";\n");
        }

        builder.addMethod(methodBuilder.build());

        JavaFile javaFile = JavaFile.builder("logger", builder.build())
                .build();
        try {
            javaFile.writeTo(processingEnv.getFiler());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
